package com.wm.dao.hibernate;

import java.sql.SQLException;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.wm.bean.UserScriptorium;
import com.wm.comm.Constant;
import com.wm.dao.UserScriptoriumDAO;

/**
 * @author yangbo
 *
 * 2012-12-26
 */
public class UserScriptoriumHibernateDAO extends HibernateDaoSupport implements UserScriptoriumDAO {
	
	private static final String LOAD_BY_ID = "from UserScriptorium where id = ?";
	private static final String REMOVE_BY_ID = "delete UserScriptorium where id = ?";
	private static final String LOAD_BY_SCR_NAME = "from UserScriptorium where scrName link %'?'%";
	
	/**
	 * 保存UserScriptorium对象
	 * 
	 * @param us
	 * 			UserScriptorium
	 * @return  UserScriptorium
	 * 			UserScriptorium
	 */
	public UserScriptorium saveUserScriptorium(UserScriptorium us){
		this.getHibernateTemplate().save(us);
		return us;
	}
	
	/**
	 * 更新UserScriptorium对象
	 * 
	 * @param us
	 * 			UserScriptorium
	 * @return  UserScriptorium
	 * 			UserScriptorium
	 */
	public UserScriptorium updateUserScriptorium(UserScriptorium us){
		this.getHibernateTemplate().update(us);
		return us;
	}
	
	/**
	 * 根据ID删除UserScriptorium对象
	 * 
	 * @param id
	 * 			long
	 */
	public void removeUserScriptoriumById(final long id){
		getHibernateTemplate().execute(new HibernateCallback() {
	        public Object doInHibernate(Session s) throws HibernateException, SQLException {
	          Query query = s.createQuery(REMOVE_BY_ID);
	          query.setLong(0, id);
	          query.executeUpdate();
	          return null;
	        }
	      });
	}
	
	/**
	 * 根据ID获取UserScriptorium对象
	 * 
	 * @param id
	 * 			long
	 * @return  UserScriptorium
	 * 			UserScriptorium
	 */
	public UserScriptorium findUserScriptoriumById(long id){
		List l = this.getHibernateTemplate().find(LOAD_BY_ID,id);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return (UserScriptorium) l.get(0);
		}
	}
	
	/**
	 * 获取UserScriptorium对象个数
	 * @return
	 */
	public long getUserScriptoriumNum(){
		StringBuffer sb = new StringBuffer();
		sb.append("select count(id) from UserScriptorium where 1=1");
		List l = this.getHibernateTemplate().find(sb.toString());
		if (l == null || l.isEmpty()) {
			return 0;
		} else {
			return (Long) l.get(0);
		}
	}
	
	/**
	 * 获取UserScriptorium对象列表
	 * 
	 * @param orderby
	 *            String
	 * @param ascOrDesc
	 *            int
	 * @param firstResult
	 *            int
	 * @param maxResults
	 *            int
	 * @return List
	 * 			UserScriptorium对象列表
	 */
	public List findUserScriptoriums(String orderby, int ascOrDesc, final int firstResult, final int maxResults){
		StringBuffer sb = new StringBuffer();
		sb.append("from UserScriptorium where 1=1");
		if (StringUtils.isNotBlank(orderby)) {
			if (ascOrDesc == Constant.ORDER_ASC) {
				sb.append(" order by " + orderby+" asc");
			}
			if (ascOrDesc == Constant.ORDER_DESC) {
				sb.append(" order by " + orderby+" desc");
			}
		}
		final String sql = sb.toString();
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException, SQLException {
				Query query = s.createQuery(sql);
				query.setFirstResult(firstResult);
				query.setMaxResults(maxResults);
				List list = query.list();
				return list;
			}
		});
	}
	
	/**
	 * 根据scrName查询
	 * @param scrName
	 * @return
	 */
	public List findUserScriptoriumByscrName(String scrName){
		return this.getHibernateTemplate().find(LOAD_BY_SCR_NAME,scrName);
	}
}
